﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.Security;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.DAL;
using Gift.Model;

using Gift.Model.crm.eNum;
namespace WebSite.crm.admin
{
    public partial class adminAdd : BaseCrm
    {

        // Methods

        protected void Page_Load(object sender, EventArgs e)
        {
         

            if (!IsPostBack)
            {
                //HttpCookie themeCookie = new HttpCookie("Theme", "access");
                //themeCookie.Expires = DateTime.Now.AddYears(1);
                //Response.Cookies.Add(themeCookie);

                //ExtAspNet.PageContext.Refresh();
                //if (Request.QueryString["uid"] != AdminUserId)
                //{
                //   
                //}
                base.ValidateLoginRule(eRule.CompanyManager);
                if (base.Request.QueryString["uid"] != null)
                {
                    this.txtLoginName.Readonly = true;
                    this.upbind();
                }
            }

        }


        private void upbind()
        {
            string sql = "select top 1 * from db_adminLogin where Adminid=" + base.Request.QueryString["uid"];
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            this.txtLoginName.Text = table.Rows[0]["AdminUser"].ToString();
            this.txtUserName.Text = table.Rows[0]["AdminName"].ToString();

            this.txtRemark.Text = table.Rows[0]["AdminDesc"].ToString();
        }

        protected void btnSubmitForm1_Click(object sender, EventArgs e)
        {
            if (base.Request.QueryString["uid"] != null)
            {
                string str2;
                DataTable table = SQLAccess.GreatDs("SELECT AdminPwd FROM db_adminLogin where Adminid=" + base.Request.QueryString["uid"] + "").Tables[0];
                if (re.replace(this.txtLoginPassword.Text).ToString() == table.Rows[0]["AdminPwd"].ToString())
                {
                    str2 = re.replace(this.txtLoginPassword.Text);
                }
                else
                {
                    str2 = FormsAuthentication.HashPasswordForStoringInConfigFile(re.replace(this.txtLoginPassword.Text), "MD5");
                }
                SQLAccess.DoSql("update db_adminLogin set AdminUser='" + re.replace(this.txtLoginName.Text) + "',AdminPwd='" + str2 + "',AdminName='" + re.replace(this.txtUserName.Text) + "',AdminFlag=1,AdminDesc='" + re.replace(this.txtRemark.Text) + "' where Adminid=" + base.Request.QueryString["uid"] + "");
                ExtAspNet.Alert.Show("修改成功！", "", "openNewTab('AdminManager','/crm/admin/adminManager.aspx','管理员管理','AdminEdit')");

            }
            else
            {
                DataTable table2 = SQLAccess.GreatDs("SELECT AdminId FROM db_adminLogin where AdminUser='" + re.replace(this.txtUserName.Text) + "'").Tables[0];
                if (table2.Rows.Count > 0)
                {
                    ExtAspNet.Alert.Show("对不起，此用户已存在，请用别的用户名！");
                }
                else
                {
                    SQLAccess.DoSql("insert into db_adminLogin(AdminUser,AdminPwd,AdminName,AdminFlag,AdminDesc,AdminTypeId) values ('" + re.replace(this.txtLoginName.Text) + "','" + FormsAuthentication.HashPasswordForStoringInConfigFile(re.replace(this.txtLoginPassword.Text), "MD5") + "','" + re.replace(this.txtUserName.Text) + "',1,'" + re.replace(this.txtRemark.Text) + "',5)");
                    ExtAspNet.Alert.Show("添加成功！", "", "openNewTab('AdminManager','/crm/admin/adminManager.aspx','管理员管理','AdminAdd')");
                }
            }
        }
    }
}